package com.analyse.stock.model;

import cn.hutool.core.date.DateUtil;
import com.analyse.stock.utils.StockStringUtil;
import com.analyse.stock.vo.FinanceDataVO;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.Objects;

@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("t_board_index_daily")
public class BoardIndexDailyModel extends BaseModel<BoardCategoryModel> {

    /**
     * 板块/指标 编码
     */
    private String code;
    /**
     * 编码类别 0-板块，1-指数
     */
    private Integer codeType;

    /**
     * 交易日期
     */
    private Date tradeDate;

    /**
     * 最新价格
     */
    private BigDecimal price;

    //今日开盘价格
    private BigDecimal openPrice;
    //当日最高价格
    private BigDecimal highPrice;
    //当日最低价格
    private BigDecimal lowerPrice;
    //昨日收盘价格
    private BigDecimal preClosePrice;
    /**
     * 板块/指标的涨跌额度
     */
    private BigDecimal upDownAmt;

    /**
     * 板块/指标的涨跌幅度
     */
    private BigDecimal upDownPercent;

    /**
     * 流通总市值
     */
    private BigDecimal tradeMv;

    /**
     * 流通总股本
     */
    private BigDecimal tradeCapitalStock;

    /**
     * 换手率
     */
    private BigDecimal exchangeRate;

    /**
     * 上涨家数
     */
    private Integer upStockCount;

    /**
     * 下跌家数
     */
    private Integer downStockCount;

    /**
     * 平盘家数
     */
    private Integer peaceStockCount;

    /**
     * 成交量
     */
    private BigDecimal tradeVolume;

    /**
     * 成交额
     */
    private BigDecimal tradeAmount;

    /**
     * 量比
     */
    private BigDecimal volumePercent;

    /**
     * 主力净流入
     */
    private BigDecimal mainAmt;

    /**
     * 主力流入
     */
    private BigDecimal mainInAmt;

    /**
     * 主力流出
     */
    private BigDecimal mainOutAmt;

    /**
     * 年分区
     */
    private Integer partitionKey;

    public static BoardIndexDailyModel transBoardIndexDailyModel(FinanceDataVO dataVO,Integer codeType, String boardCode) {
        if(Objects.isNull(dataVO)){
            return null;
        }
        BoardIndexDailyModel model = new BoardIndexDailyModel();
        model.setCodeType(codeType);
        model.setCode(boardCode);
        model.setTradeDate(new Date());
        model.setPartitionKey(DateUtil.year(model.getTradeDate()));
        BigDecimal f43 = StockStringUtil.getDecimalValue(dataVO.getF43()).divide(new BigDecimal(100),2,RoundingMode.HALF_UP);
        model.setPrice(f43);
        BigDecimal f44 = StockStringUtil.getDecimalValue(dataVO.getF44()).divide(new BigDecimal(100),2,RoundingMode.HALF_UP);
        model.setHighPrice(f44);
        BigDecimal f45 = StockStringUtil.getDecimalValue(dataVO.getF45()).divide(new BigDecimal(100),2,RoundingMode.HALF_UP);
        model.setLowerPrice(f45);
        BigDecimal f46 = StockStringUtil.getDecimalValue(dataVO.getF46()).divide(new BigDecimal(100),2,RoundingMode.HALF_UP);
        model.setOpenPrice(f46);
        BigDecimal f60 = StockStringUtil.getDecimalValue(dataVO.getF60()).divide(new BigDecimal(100),2,RoundingMode.HALF_UP);
        model.setPreClosePrice(f60);

        //昨天收盘（分单位）
        //BigDecimal f60 = StockStringUtil.getDecimalValue(dataVO.getF60());
        //今日涨跌额（分单位） = 昨天收盘 - 今日最新
        //BigDecimal upDownAmt = (f60.subtract(StockStringUtil.getDecimalValue(dataVO.getF43())));
        //今日涨跌额（元单位）=（昨天收盘 - 今日最新/100
        BigDecimal f169 = StockStringUtil.getDecimalValue(dataVO.getF169());
        model.setUpDownAmt(f169.divide(new BigDecimal(100),2,RoundingMode.HALF_UP));
        //涨跌幅% = 今日涨跌额/昨天收盘
        BigDecimal f170 = StockStringUtil.getDecimalValue(dataVO.getF170());
        model.setUpDownPercent(f170.divide(new BigDecimal(100),2,RoundingMode.HALF_UP));
        //流通总市值
        model.setTradeMv(StockStringUtil.getDecimalValue(dataVO.getF117()));
        //流通总股本
        model.setTradeCapitalStock(StockStringUtil.getDecimalValue(dataVO.getF85()));

        model.setTradeVolume(StockStringUtil.getDecimalValue(dataVO.getF47()));
        model.setTradeAmount(StockStringUtil.getDecimalValue(dataVO.getF48()));
        //量比
        model.setVolumePercent(StockStringUtil.getDecimalValue(dataVO.getF50()).divide(new BigDecimal(100),2,RoundingMode.HALF_UP));
        model.setUpStockCount(Integer.parseInt(dataVO.getF113()));
        model.setDownStockCount(Integer.parseInt(dataVO.getF114()));
        model.setPeaceStockCount(Integer.parseInt(dataVO.getF115()));

        BigDecimal f168 = new BigDecimal(dataVO.getF168()).divide(new BigDecimal(100),2,RoundingMode.HALF_UP);
        model.setExchangeRate(f168);

        model.setMainAmt(StockStringUtil.getDecimalValue(dataVO.getF135()));
        model.setMainOutAmt(StockStringUtil.getDecimalValue(dataVO.getF136()));
        model.setMainInAmt(StockStringUtil.getDecimalValue(dataVO.getF137()));
        model.setUpdateTime(new Date());
        model.setCreateTime(new Date());
        return model;
    }
}

